package com.seu.vcampus.message;

import java.io.Serializable;

/**
 * 网络传输消息类
 * 客户端和服务器之间的通信协议
 * 
 * @author 虚拟校园开发团队
 * @since 1.0.0
 */
public class Message implements Serializable {
    private static final long serialVersionUID = 1L;
    
    public enum Type {
        LOGIN_REQUEST,
        LOGIN_RESPONSE,
        REGISTER_REQUEST,
        REGISTER_RESPONSE,
        LOGOUT_REQUEST,
        LOGOUT_RESPONSE,
        UPDATE_USER_REQUEST,
        UPDATE_USER_RESPONSE,
        DELETE_USER_REQUEST,
        DELETE_USER_RESPONSE,
        GET_USER_REQUEST,
        GET_USER_RESPONSE,
        // 权限相关消息类型
        GET_PERMISSIONS_REQUEST,
        GET_PERMISSIONS_RESPONSE,
        CHECK_PERMISSION_REQUEST,
        CHECK_PERMISSION_RESPONSE,
        ADD_PERMISSION_REQUEST,
        ADD_PERMISSION_RESPONSE,
        REMOVE_PERMISSION_REQUEST,
        REMOVE_PERMISSION_RESPONSE,
        // 图书馆相关消息类型
        GET_BOOKS_REQUEST,
        GET_BOOKS_RESPONSE,
        SEARCH_BOOKS_REQUEST,
        SEARCH_BOOKS_RESPONSE,
        BORROW_BOOK_REQUEST,
        BORROW_BOOK_RESPONSE,
        RETURN_BOOK_REQUEST,
        RETURN_BOOK_RESPONSE,
        GET_BORROW_RECORDS_REQUEST,
        GET_BORROW_RECORDS_RESPONSE,
        GET_ALL_BORROW_RECORDS_REQUEST,
        GET_ALL_BORROW_RECORDS_RESPONSE,
        ADD_BOOK_REQUEST,
        ADD_BOOK_RESPONSE,
        UPDATE_BOOK_REQUEST,
        UPDATE_BOOK_RESPONSE,
        DELETE_BOOK_REQUEST,
        DELETE_BOOK_RESPONSE,
        GET_BOOK_BY_ID_REQUEST,
        GET_BOOK_BY_ID_RESPONSE,
        CHECK_BOOK_BORROWED_REQUEST,
        CHECK_BOOK_BORROWED_RESPONSE,
        GET_MY_BORROW_RECORDS_REQUEST,
        GET_MY_BORROW_RECORDS_RESPONSE,
        //商店相关消息
        GET_PRODUCTS_REQUEST, 
        GET_PRODUCTS_RESPONSE,
        GET_PRODUCT_BY_ID_REQUEST,
        GET_PRODUCT_BY_ID_RESPONSE,
        GET_PRODUCT_BY_NAME_REQUEST,
        GET_PRODUCT_BY_NAME_RESPONSE,
        SEARCH_PRODUCT_REQUEST,
        SEARCH_PRODUCT_RESPONSE,
        SAVE_PRODUCT_REQUEST,
        SAVE_PRODUCT_RESPONSE,
        DELETE_PRODUCT_REQUEST, 
        DELETE_PRODUCT_RESPONSE,
        GET_PRODUCT_COUNT_REQUEST,
        GET_PRODUCT_COUNT_RESPONSE,
        UPDATE_PRODUCT_STOCK_REQUEST,
        UPDATE_PRODUCT_STOCK_RESPONSE,
        GET_ORDER_BY_ID_REQUEST,
        GET_ORDER_BY_ID_RESPONSE,
        GET_USER_ORDERS_REQUEST,
        GET_USER_ORDERS_RESPONSE,
        GET_ALL_ORDERS_REQUEST, 
        GET_ALL_ORDERS_RESPONSE,
        DELETE_SINGLE_ORDER_REQUEST,
        DELETE_SINGLE_ORDER_RESPONSE,
        DELETE_USER_ORDERS_REQUEST,
        DELETE_USER_ORDERS_RESPONSE,
        GET_USER_ORDER_COUNT_REQUEST,
        GET_USER_ORDER_COUNT_RESPONSE,
        GET_USER_CART_REQUEST,
        GET_USER_CART_RESPONSE,
        UPDATE_USER_CARTITEM_REQUEST,
        UPDATE_USER_CARTITEM_RESPONSE,
        BUY_PRODUCT_REQUEST,
        BUY_PRODUCT_RESPONSE,
        ADD_TO_CART_REQUEST,
        ADD_TO_CART_RESPONSE,
        ERROR
    }
    
    private Type type;
    private Object payload;
    private String sessionId;
    private boolean success;
    private String errorMessage;
    
    public Message() {}
    
    public Message(Type type) {
        this.type = type;
    }
    
    public Message(Type type, Object payload) {
        this.type = type;
        this.payload = payload;
    }
    
    // Getters and Setters
    public Type getType() { return type; }
    public void setType(Type type) { this.type = type; }
    
    public Object getPayload() { return payload; }
    public void setPayload(Object payload) { this.payload = payload; }
    
    public String getSessionId() { return sessionId; }
    public void setSessionId(String sessionId) { this.sessionId = sessionId; }
    
    public boolean isSuccess() { return success; }
    public void setSuccess(boolean success) { this.success = success; }
    
    public String getErrorMessage() { return errorMessage; }
    public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }
    
    public static Message success(Type type, Object payload) {
        Message msg = new Message(type, payload);
        msg.setSuccess(true);
        return msg;
    }
    
    public static Message error(String errorMessage) {
        Message msg = new Message(Type.ERROR);
        msg.setSuccess(false);
        msg.setErrorMessage(errorMessage);
        return msg;
    }
    
    @Override
    public String toString() {
        return "Message{" +
                "type=" + type +
                ", success=" + success +
                ", payload=" + payload +
                '}';
    }
}